<?php
class Control_Announce_saveNew {
	function saveNew() {
		  $tagId		= MooForm::request('tagId');
		  $minTitle	= MooForm::request('minTitle');
		  $nameKey	= MooForm::request('nameKey');
		  $content 	= MooForm::request('content');
		  $title 	= MooForm::request('title');
		  
		  /*
		  $version = "baidu";
		  $minTitle = "版本优化内容";
		  $nameKey = "2015-06-01_16-26-46";
		  $content ='okdd';
		  */
		//  $content = str_replace('<p', '<p style="margin:10px"', $content);
		  
		  $fileName = $tagId . "_" . $nameKey;
		  
		  $keyTitle = array();
		  // 记录keyName => title
		  $noticesPath = ROOT_PATH . "/notices/";
		  if (!MooFile::isExists($noticesPath)) {
			 MooFile::mkDir($noticesPath);
		  }
		  $keyTitleFile = $noticesPath . "keyTitle.txt";
		  $keyTitle[$nameKey] = $title;
		  if(!file_exists($keyTitleFile)) {
		  	 MooFile::write($keyTitleFile, MooJson::encode($keyTitle)); // 初次写入keyTitle	
		  } else {
		  	 $keyTitleJson = MooFile::readAll($keyTitleFile);
		  	 $keyTitleArr = MooJson::decode($keyTitleJson);
		  	 $keyTitleArr[$nameKey] = $title;
		  	 $keyNameJson = MooJson::encode($keyTitleArr);
		  	 MooFile::write($keyTitleFile,  $keyNameJson); // 将本次title写入keyTitle
		  }
		  
	  	  $filePath = ROOT_PATH . "/notices/" . $fileName . ".txt";
	  	 
		  $content	= 	str_replace("\n",' ', $content);
		  $content	= 	str_replace("|","'", $content);
		  
		  $index = 1;
		  if(!file_exists($filePath)) {
		  		$content = "id	title	content" . "\n" . $index . "	" . $minTitle . "	" . $content;	// $作为换行符
		  		$rs = MooFile::write($filePath, $content);
		  } else {
		  		$nowContents = MooFile::readAll($filePath);
		  		$num = substr_count($nowContents, "\n");
		  		$data = array();
		  		while(false !== ($line = MooFile::readLine($filePath))) {
		  			if (!$line) {
		  				continue;
		  			}
		  			$tmpArr = array();
		  			$lineArr = explode('	', $line);
		  			// 修复程序 这一行至少大于3
		  			if(count($lineArr) >= 3) {
		  				$data[$lineArr[1]] = $lineArr;
		  			}
		  		}
		  		
		  		// 修改 如果已经有该小标题 则直接更新
		  		if($data[$minTitle]) {
		  			$index = $data[$minTitle][0]; // 原来的id 是几还是几
		  			unset($data[$minTitle]);
		  			// $index = $num;
		  			// $content = $index . "	" . $minTitle . "	" . $content;
		  			
		  			//$data[$minTitle][1] = $minTitle;
		  			$data[$minTitle][0] = $index;
		  			$data[$minTitle][1] = $minTitle;
		  			$data[$minTitle][2] = $content;
		  			
		  			$dataValues = array();
		  			foreach ($data as $k => $v) {
		  				if($v[0] == "id") {
		  					$index = 0;
		  				} else {
		  					$index = $v[0];
		  				}
		  				$dataValues[$index] = $v;
		  			}
		  			
		  			ksort($dataValues);
		  			
		  			$lineNum = count($dataValues);
		  			$str = "";
		  			foreach ($dataValues as $key => $val) {
		  				$line=implode("	", $val);
		  				$num = substr_count($line, "\n");
						
		  				// 最后一行不要用 "\n"
		  				if($key < $lineNum - 1 && $num == 0) {
		  					$line = $line . "\n"; 
		  				}
		  				
		  				$str = $str . $line;
		  			}
		  			
		  			// 更新
		  			$rs = MooFile::write($filePath, $str);
		  			
		  		} else {
		  			
		  			$index = $num + 1;
		  			$content = "\n" . $index . "	" . $minTitle . "	" . $content;
		  			
		  			// 追加
		  			$rs = MooFile::write($filePath, $content, true);
		  		}
		  }
		 
		  if ($rs) {
		  	exit("保存成功");
		  } else {
		  	exit("保存失败");
		  }
	}
}